var Console = require("Console")
var init = {
    init(){
        // cc.audioEngine.stopAll = function(){};
        this.voice();
        this.bgm();
        this.shareBg();
        this.gameIcon();
        this.gameList();
    },

    voice: function(){
         var obj = {
            bol: false,
            url: {},
            isStart: true,
            volume: 1,
            playEffect: function(url, loop){
                if(this.volume == 0) return;
                if(this.isStart){
                    cc.audioEngine.play(this.url[url], loop, this.volume);
                }
            },

            loader: function() {
                if(this.bol) return;
                var self = this;
                cc.loader.loadResDir("audio", function(err, assets){
                    if(err) return ;
                    assets.forEach(function(ele){
                        var name = ele.split("/").pop().replace(/^(.+)(\..+)$/, "$1")
                        cc.voicePlay.url[name] = ele;
                        cc.audioEngine.preload(ele, function(){});
                    })
                    self.bol = true;
                })
            }
        }
         cc.voicePlay = cc.voicePlay || obj;
    },

    bgm(){
        var obj = {
            bol: false,
            loader(voice){
                if(this.bol) return;
                var self = this;
                cc.loader.loadResDir("bgm", function(err, assets){
                    if(err) return ;
                    self.data = assets[0];
                    if(voice) cc.audioEngine.play(assets[0], true, 0.2);
                    self.bol = true;
                })
            },

            play(){
                if(cc.gameStart.voiceControl){
                    clearInterval(cc.gameStart.voiceControl)
                    cc.audioEngine.stopAll();
                }
                if(this.bol){
                    cc.audioEngine.play(this.data, true, 0.2);
                }else{
                    // this.bol = false;
                    this.loader(true);
                }
            }
        }
        cc.bgm = cc.bgm || obj;
    },
    shareBg(){
        var obj = {
            bol: false,
            loader: function(){
                if(this.bol) return ;
                var self = this;
                cc.loader.loadResDir("shareBg", cc.SpriteFrame, function(err, assets){
                    if(err) return ;
                    try{
                        shareImg.bgUrl = "./" + assets[0]._textureFilename;
                        shareImg.headUrl = FBInstant.player.getPhoto();
                        shareImg.init();
                    }catch(err){}
                    self.bol = true;
                })
            }
        }

        cc.shareBg = cc.shareBg || obj
    },
    gameList(data){
        var obj = {
            bol: false,
            games: null,
            AdTime: null,
            loader: function(data){
                if(this.bol) return ;
                var self = this;
                cc.loader.load("https://s3-us-west-1.amazonaws.com/res.facebook.in/config/facebookgames.json", function(err, assets){
                    if(err) return ;
                    self.games = assets.games;
                    self.AdTime = assets.AdTime;
                    data && data.show();
                    self.games.forEach(function(ele){
                        cc.gameIcon.loader(ele);
                    })
                    self.bol = true;
                })
            }
        };

        cc.gameList = cc.gameList || obj
    },

    gameIcon(){
        var obj = {
            bol: false,
            icon:{},
            loader: function(data, fn){
                var self = this, icon = "config/circles.png";
                if(self.icon[data.AppId]) return;
                if(data.icon){
                    icon = data.icon + "/App Icon.jpg";
                }
                cc.loader.load("https://s3-us-west-1.amazonaws.com/res.facebook.in/icon/" + icon, function(err, assets){
                    if(err) return ;
                    var spriteFrame = new cc.SpriteFrame(assets);
                    self.icon[data.AppId] = spriteFrame;
                    fn && fn.show(data);
                })
            }
        };

        cc.gameIcon = cc.gameIcon || obj
    },


};
init.init();
module.exports = init;
